<?php


function copy_r($dir,$dest,$v=0,$s=false) {
    if (!is_dir($dest)) {
        if (!$s) mkdir($dest);
        if ($v) echo 'mkdir '.$dest.'<br />'."\n";
    }
    if ( (!$s) && (!is_writable($dest)) ) {
        echo '<strong>Fail ['.$dest.']</strong><br />'."\n";
        return false;
    }
    
    $handle = opendir($dir);
    while ($f = readdir($handle)) {
        if (is_dir($dir.$f)) {
            if ( ($f != '.') && ($f != '..') && ($f != '.svn') ) {
                if (!copy_r($dir.$f.'/',$dest.$f.'/',$v,$s)) return false;
            } elseif ($v>1) echo 'Skipped folder - ' . $dir.$f.'<br />'."\n";
        } else {
            if (strpos($f,'~')) {
                if ($v>1) echo 'Skipped file - ' . $dir.$f.'<br />'."\n";
            } else {
                if (!$s) copy($dir.$f,$dest.$f);
                if ($v) echo 'cp '.$dir.$f.' '.$dest.$f.'<br />'."\n";
            }
        }
    }
    return true;
}



function delete_r($dir) {
    $handle = opendir($dir);
    while ($f = readdir($handle)) {
        if (is_dir($dir.$f)) {
            if ( ($f != '.') && ($f != '..') ) {
                delete_r($dir.$f.'/');
                rmdir($dir.$f.'/');
            }
        } else {
            unlink($dir.$f);
        }
    }
}


if (!is_dir('./storage/')) {
    if (copy_r('../storage/','./storage/')) {
        delete_r('../storage/');
        copy_r('./default/','../storage/');
    } else exit;
}

require_once('./header.php');

if (get_config('baseurl')=='../') {
    $lib_file = load_file('.../lib/lib.php');
    $file = $lib_file->read('array');
    $file[0] = '<?php $GLOBALS[\'settings\'] = array(\'baseurl\'=>\'http://\'.$_SERVER[\'HTTP_HOST\'].\'' . foldername(dirname($_SERVER['PHP_SELF'])) . '\',\'basepath\'=>dirname(dirname(__FILE__)) . \'/\');'."\n";
    $lib_file->write($file);
}
?>
        <h1>Upgrading ComicCMS</h1>
        <p>We are about to upgrade from ComicCMS version <?php echo $v; ?> to version 0.2. This is a big upgrade and will require some human input, so please read everything you see on the screen as you go.</p>
        <p>Seriously now, slow down your clicking finger and pay attention. Upgrade actions are not reversable. Do not go back in your browser just follow the links you want.</p>
        <p>First off your comics, news posts and users are going to be converted over. This is fully automated and should go off without a hitch.</p>
        <p><a href="step1.php">So, on to stage one where we'll convert your comics over. Click me!</a></p>
    </div>
</body>
</html>